package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

import dao.StudentListDAO;
import dao.UserDAO;
import dao.impl.StudentListDAOImpl;
import dao.impl.UserDAOImpl;
import entity.JsonMsg;
import entity.User;
import service.StudentListService;
import service.UserService;
import service.impl.StudentListServiceImpl;
import service.impl.UserServiceImpl;
import utils.DBUtil;

/**
 * Servlet implementation class IdServlet
 */
@WebServlet("/IdServlet")
public class IdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public IdServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);

		String op = request.getParameter("op");

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");

		String id = request.getParameter("id");
		String sql = "select *from tab_studentlists where studentlistid=?";
		DBUtil dbUtil = new DBUtil();
		ResultSet rs = dbUtil.query(sql, id);
		JsonMsg jsonMsg = null;
		UserService userService = new UserServiceImpl();
		StudentListService studentlistservice = new StudentListServiceImpl();
		StudentListDAO studentListdao = new StudentListDAOImpl();
		UserDAO userDAO = new UserDAOImpl();
		System.out.println(studentListdao.findByStudentListId(id));
		if (studentListdao.findByStudentListId(id) == null) {

			jsonMsg = JsonMsg.makeSuccess("未知用户，不存在学生数据库中", null);
		}
		ObjectMapper objectMapper2 = new ObjectMapper();
		String writeValueAsString = objectMapper2.writeValueAsString(jsonMsg);
		out.println(writeValueAsString);

		out.flush();
		out.close();
	}

}
